<template>
  <div class="picture-search-form">
    <!-- 搜索表单 -->
    <a-form name="searchForm" layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="关键词" name="searchText">
        <a-input
          v-model:value="searchParams.searchText"
          placeholder="从名称和简介搜索"
          allow-clear
        />
      </a-form-item>
      <a-form-item name="category" label="分类">
        <a-auto-complete
          v-model:value="searchParams.category"
          style="min-width: 180px"
          placeholder="请输入分类"
          :options="categoryOptions"
          allow-clear
        />
      </a-form-item>
      <a-form-item name="tags" label="标签">
        <a-select
          v-model:value="searchParams.tags"
          style="min-width: 180px"
          mode="tags"
          placeholder="请输入标签"
          :options="tagOptions"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="日期" name="dateRange">
        <a-range-picker
          style="width: 400px"
          show-time
          v-model:value="dateRange"
          :placeholder="['编辑开始时间', '编辑结束时间']"
          format="YYYY/MM/DD HH:mm:ss"
          :presets="rangePresets"
          @change="onRangeChange"
        />
      </a-form-item>
      <a-form-item label="名称" name="name">
        <a-input v-model:value="searchParams.name" placeholder="请输入名称" allow-clear />
      </a-form-item>
      <a-form-item label="简介" name="introduction">
        <a-input v-model:value="searchParams.introduction" placeholder="请输入简介" allow-clear />
      </a-form-item>
      <a-form-item label="宽度" name="picWidth">
        <a-input-number v-model:value="searchParams.picWidth" />
      </a-form-item>
      <a-form-item label="高度" name="picHeight">
        <a-input-number v-model:value="searchParams.picHeight" />
      </a-form-item>
      <a-form-item label="格式" name="picFormat">
        <a-input v-model:value="searchParams.picFormat" placeholder="请输入格式" allow-clear />
      </a-form-item>
      <a-form-item>
        <a-space>
          <a-button type="primary" html-type="submit" style="width: 96px">搜索</a-button>
          <a-button html-type="reset" @click="doClear">重置</a-button>
        </a-space>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import dayjs from 'dayjs'
import {listPictureTagCategoryUsingGet, listPictureVoByPageUsingPost} from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'

interface Props {
  onSearch?: (searchParams: API.PictureQueryRequest) => void
}

const props = defineProps<Props>()

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({})

// 搜索数据
const doSearch = () => {
  props.onSearch?.(searchParams)
}

// 标签和分类选项
const categoryOptions = ref<string[]>([])
const tagOptions = ref<string[]>([])

/**
 * 获取标签和分类选项
 * @param values
 */
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    tagOptions.value = (res.data.data.tagList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  } else {
    message.error('获取标签分类列表失败，' + res.data.message)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})

const dateRange = ref<[]>([])

/**
 * 日期范围更改时触发
 * @param dates
 * @param dateStrings
 */
const onRangeChange = (dates: any[], dateStrings: string[]) => {
  if (dates?.length >= 2) {
    searchParams.startEditTime = dates[0].toDate()
    searchParams.endEditTime = dates[1].toDate()
  } else {
    searchParams.startEditTime = undefined
    searchParams.endEditTime = undefined
  }
}

// 时间范围预设
const rangePresets = ref([
  { label: '过去 7 天', value: [dayjs().add(-7, 'd'), dayjs()] },
  { label: '过去 14 天', value: [dayjs().add(-14, 'd'), dayjs()] },
  { label: '过去 30 天', value: [dayjs().add(-30, 'd'), dayjs()] },
  { label: '过去 90 天', value: [dayjs().add(-90, 'd'), dayjs()] },
])

// 清理
const doClear = () => {
  // 取消所有对象的值
  Object.keys(searchParams).forEach((key) => {
    searchParams[key] = undefined
  })
  // 日期筛选项单独清空，必须定义为空数组
  dateRange.value = []
  // 清空后重新搜索
  props.onSearch?.(searchParams)
}
</script>

<style scoped>
.picture-search-form {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  padding: 1.5rem;
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.picture-search-form .ant-form-item {
  margin-top: 16px;
}

/* 黑色模式下的搜索表单样式 */
[data-theme="dark"] .picture-search-form {
  background: rgba(31, 41, 55, 0.95);
  border: 1px solid rgba(75, 85, 99, 0.3);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* 黑色模式下的表单标签 */
[data-theme="dark"] .picture-search-form :deep(.ant-form-item-label > label) {
  color: #e5e7eb !important;
}

/* 黑色模式下的输入框 */
[data-theme="dark"] .picture-search-form :deep(.ant-input),
[data-theme="dark"] .picture-search-form :deep(.ant-input-number),
[data-theme="dark"] .picture-search-form :deep(.ant-input-number-input),
[data-theme="dark"] .picture-search-form :deep(.ant-select-selector),
[data-theme="dark"] .picture-search-form :deep(.ant-picker),
[data-theme="dark"] .picture-search-form :deep(.ant-auto-complete .ant-input) {
  background: rgba(55, 65, 81, 0.8) !important;
  border-color: rgba(75, 85, 99, 0.5) !important;
  color: #e5e7eb !important;
}

/* 黑色模式下的输入框悬停状态 */
[data-theme="dark"] .picture-search-form :deep(.ant-input:hover),
[data-theme="dark"] .picture-search-form :deep(.ant-input-number:hover),
[data-theme="dark"] .picture-search-form :deep(.ant-input-number:hover .ant-input-number-input),
[data-theme="dark"] .picture-search-form :deep(.ant-select:hover .ant-select-selector),
[data-theme="dark"] .picture-search-form :deep(.ant-picker:hover),
[data-theme="dark"] .picture-search-form :deep(.ant-auto-complete:hover .ant-input) {
  border-color: rgba(14, 165, 233, 0.6) !important;
}

/* 黑色模式下的输入框聚焦状态 */
[data-theme="dark"] .picture-search-form :deep(.ant-input:focus),
[data-theme="dark"] .picture-search-form :deep(.ant-input-focused),
[data-theme="dark"] .picture-search-form :deep(.ant-input-number-focused),
[data-theme="dark"] .picture-search-form :deep(.ant-input-number-focused .ant-input-number-input),
[data-theme="dark"] .picture-search-form :deep(.ant-select-focused .ant-select-selector),
[data-theme="dark"] .picture-search-form :deep(.ant-picker-focused),
[data-theme="dark"] .picture-search-form :deep(.ant-auto-complete-focused .ant-input) {
  border-color: #0ea5e9 !important;
  box-shadow: 0 0 0 2px rgba(14, 165, 233, 0.2) !important;
}

/* 黑色模式下的占位符文字 */
[data-theme="dark"] .picture-search-form :deep(.ant-input::placeholder),
[data-theme="dark"] .picture-search-form :deep(.ant-input-number-input::placeholder),
[data-theme="dark"] .picture-search-form :deep(.ant-select-selection-placeholder),
[data-theme="dark"] .picture-search-form :deep(.ant-auto-complete .ant-input::placeholder) {
  color: #9ca3af !important;
}

/* 黑色模式下的选择项 */
[data-theme="dark"] .picture-search-form :deep(.ant-select-selection-item) {
  color: #e5e7eb !important;
  background: rgba(14, 165, 233, 0.2) !important;
  border-color: rgba(14, 165, 233, 0.3) !important;
}

/* 黑色模式下的按钮 */
[data-theme="dark"] .picture-search-form :deep(.ant-btn:not(.ant-btn-primary)) {
  background: rgba(55, 65, 81, 0.8) !important;
  border-color: rgba(75, 85, 99, 0.5) !important;
  color: #e5e7eb !important;
}

[data-theme="dark"] .picture-search-form :deep(.ant-btn:not(.ant-btn-primary):hover) {
  background: rgba(75, 85, 99, 0.8) !important;
  border-color: rgba(14, 165, 233, 0.6) !important;
  color: #f3f4f6 !important;
}
</style>
